package com.javaclimb.filmbuy.controller;

import com.github.pagehelper.PageInfo;
import com.javaclimb.filmbuy.common.Result;
import com.javaclimb.filmbuy.entity.AdvertiserInfo;
import com.javaclimb.filmbuy.entity.MessageInfo;
import com.javaclimb.filmbuy.service.AdvertiserInfoService;
import com.javaclimb.filmbuy.service.MessageInfoService;
import com.javaclimb.filmbuy.vo.MessageInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/*
* 公告信息的controller层
* */
@RestController
@RequestMapping(value = "/advertiserInfo")
public class AdvertiserInfoController {
    @Resource
    private AdvertiserInfoService advertiserInfoService;
    @Autowired
    private MessageInfoService messageInfoService;

    /*
    * 增加一个公告信息
    * */
    @PostMapping
    public Result<AdvertiserInfo> add(@RequestBody AdvertiserInfo advertiserInfo){
        advertiserInfoService.add(advertiserInfo);
        return Result.success(advertiserInfo);
    }

    /*
    * 根据主键id删除一个公告信息
    * */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Long id){
        advertiserInfoService.delete(id);
        return Result.success();
    }

    /*
    * 更新一个公告信息
    * */
    @PutMapping
    public Result<AdvertiserInfo> update(@RequestBody AdvertiserInfo advertiserInfo) {
        advertiserInfoService.update(advertiserInfo);
        return Result.success(advertiserInfo);
    }

    /*
     * 根据主键id查询一个公告信息
     * */
    @GetMapping("/{id}")
    public Result<AdvertiserInfo> findById(@PathVariable Long id){
        AdvertiserInfo advertiserInfo = advertiserInfoService.findById(id);
        return Result.success(advertiserInfo);
    }

    /**
     * 根据公告标题模糊查询公告信息对象列表，分页查询
     * @param name 公告标题，不是必选项
     * @param pageNum 要查询第几页
     * @param pageSize 每页多少条数据
     * @return 公告信息列表
     */
    @GetMapping("/page/{name}")
    public Result<PageInfo<AdvertiserInfo>> findPage(@PathVariable String name,
                                                  @RequestParam(defaultValue = "1") Integer pageNum,
                                                  @RequestParam(defaultValue = "5") Integer pageSize){
        PageInfo<AdvertiserInfo> page = advertiserInfoService.findPage(name,pageNum,pageSize);
        return Result.success(page);
    }
    /**
     * 查询所有的在线交流，第一层是父id是0，第二层从属于第一层
     */
    @GetMapping
    public Result<List<MessageInfoVo>> findAll(){
        return Result.success(messageInfoService.findAll());
    }
}
